{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "473ad68b",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "from torchvision.transforms import Compose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "480001cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Linear(in_features=10, out_features=10, bias=True)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nn.Linear(in_features=10,out_features=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "60b41fdc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float32"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 获取张量的数据类型\n",
    "torch.tensor([1.2, 3.4]).dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e0040825",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 张量的默认数据类型设置为其它类型\n",
    "torch.set_default_tensor_type(torch.DoubleTensor)\n",
    "torch.tensor([1.2, 3.4]).dtype\n",
    "## 注意：set_default_tensor_type()函数只支持设置浮点类型数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4b7d45bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: tensor([1.2000, 3.4000])\n",
      "a.dtype: torch.float64\n",
      "a.long()方法: torch.int64\n",
      "a.int()方法: torch.int32\n",
      "a.float()方法: torch.float32\n"
     ]
    }
   ],
   "source": [
    "## 将张量数据类型转化为整型\n",
    "a = torch.tensor([1.2, 3.4])\n",
    "print(\"a:\",a)\n",
    "print(\"a.dtype:\",a.dtype)\n",
    "print(\"a.long()方法:\",a.long().dtype)\n",
    "print(\"a.int()方法:\",a.int().dtype)\n",
    "print(\"a.float()方法:\",a.float().dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "14a87225",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 张量设置成  torch.float32 类型\n",
    "torch.set_default_tensor_type(torch.FloatTensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dc7968c5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a: tensor([1.2000, 3.4000])\n",
      "a.dtype: torch.float32\n",
      "a.long()方法: torch.int64\n",
      "a.int()方法: torch.int32\n",
      "a.float()方法: torch.float32\n"
     ]
    }
   ],
   "source": [
    "## 将张量数据类型转化为整型\n",
    "a = torch.tensor([1.2, 3.4])\n",
    "print(\"a:\",a)\n",
    "print(\"a.dtype:\",a.dtype)\n",
    "print(\"a.long()方法:\",a.long().dtype)\n",
    "print(\"a.int()方法:\",a.int().dtype)\n",
    "print(\"a.float()方法:\",a.float().dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "816b9ad8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1.],\n",
      "        [2., 2.]])\n",
      "torch.Size([2, 2])\n",
      "torch.Size([2, 2])\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "## 生成张量\n",
    "### 基本方法\n",
    "A = torch.tensor([[1.0,1.0],[2,2]])\n",
    "print(A)\n",
    "\n",
    "## 获取张量的形状\n",
    "print(A.shape)\n",
    "\n",
    "## 获取张量的size\n",
    "print(A.size())\n",
    "\n",
    "## 计算张量中所含元素的个数\n",
    "print(A.numel())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "181a0e38",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B: tensor([1., 2., 3.], requires_grad=True)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([2., 4., 6.])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## 指定张量的数据类型和是否要计算梯度\n",
    "B = torch.tensor((1,2,3),dtype=torch.float32,requires_grad=True)\n",
    "print(\"B:\",B)\n",
    "\n",
    "## 因为张量B是可计算梯度的，所以可以计算sum(B^2)的梯度\n",
    "y  = B.pow(2).sum()\n",
    "y.backward()\n",
    "B.grad\n",
    "\n",
    "## 注意只有浮点类型的张量允许计算梯度\n",
    "# B = torch.tensor((1,2,3),dtype=torch.int32,requires_grad=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7af6492",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
